package linuxbox.util;

import java.io.*;
import java.util.*;
import java.text.*;
import org.jdom.*;
import org.jdom.input.*;

import linuxbox.BoxStatic;

public class BoxFormat {
    public static final String LAST = "lAST";
    public static final String FIRST = "fIRST";
    
    protected String nameFormatStr;
    protected String englishNameFormatStr;
    protected String chineseNameFormatStr;
    protected String DBTimestampFormatStr;
    protected String DBTimeFormatStr;
    protected String DBDateFormatStr;
    
    protected String postgresTimestampFormatStr;
    protected SimpleDateFormat sdFormator = new SimpleDateFormat();
    
    public BoxFormat(String fn) throws Exception {
        // get all format information from fn
        init(fn);
        if (BoxStatic.defaultLang.equalsIgnoreCase("en")) {
            nameFormatStr = englishNameFormatStr;
        } else if (BoxStatic.defaultLang.equalsIgnoreCase("cn")) {
            nameFormatStr = chineseNameFormatStr;
        } else {
            nameFormatStr = englishNameFormatStr;
        }
    }
    
    private void init(String fn) throws Exception {
        SAXBuilder builder = new SAXBuilder(false); 
        Document doc = builder.build(new FileInputStream(fn)); 
        Element root = doc.getRootElement();
        Element common = root.getChild("Common");
        for (Iterator it = common.getChildren("FormatDef").iterator(); 
        it.hasNext(); ) {
            Element el = (Element)it.next();
            String name = el.getAttributeValue("name");
            String value = el.getTextTrim();

            if (name == null || value == null) continue;

            name = name.trim();
            if ("EnglishNameFormat".equalsIgnoreCase(name)) {
                englishNameFormatStr = value;
            } else if ("ChineseNameFormat".equalsIgnoreCase(name)) {
                chineseNameFormatStr = value;
            } else if ("PostgresTimestamp".equalsIgnoreCase(name)) {
                postgresTimestampFormatStr = value;
            } else if ("DBTimestamp".equalsIgnoreCase(name)) {
                DBTimestampFormatStr = value;
            } else if ("DBTime".equalsIgnoreCase(name)) {
                DBTimeFormatStr = value;
            } else if ("DBDate".equalsIgnoreCase(name)) {
                DBDateFormatStr = value;
            }
        }
    }
        
    public String DBTime(java.sql.Timestamp ts) {
        if (ts != null) {
            sdFormator.applyPattern(DBTimeFormatStr);
            return sdFormator.format((java.util.Date)ts);
        } else 
            return null;
    }
  
    public String DBDate(java.sql.Timestamp ts) {
        if (ts != null) {
            sdFormator.applyPattern(DBDateFormatStr);
            return sdFormator.format((java.util.Date)ts);
        } else 
            return null;
    }

    public String DBTimestamp(java.sql.Timestamp ts) {
        if (ts != null) {
            sdFormator.applyPattern(DBTimestampFormatStr);
            return sdFormator.format((java.util.Date)ts);
        } else 
            return null;
    }

    public String postgresTimestamp(java.sql.Timestamp ts) {
        if (ts != null) {
            sdFormator.applyPattern(postgresTimestampFormatStr);
            return sdFormator.format((java.util.Date)ts);
        } else 
            return null;
    }
    
    public String formatName(String last, String first) {
        String formatStr = nameFormatStr;
        
        int pos = formatStr.indexOf(LAST);
        StringBuffer tmpBuf = new StringBuffer(formatStr);
        tmpBuf = tmpBuf.replace(pos, pos + 4, last);
        formatStr = tmpBuf.toString();
        
        pos = formatStr.indexOf(FIRST);
        tmpBuf = new StringBuffer(formatStr);
        tmpBuf = tmpBuf.replace(pos, pos + 5, first);
        formatStr = tmpBuf.toString();
        
        return formatStr;
    }
}